
//
//  CAReturnGoodsExpressViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/26.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAReturnGoodsExpressViewController.h"
#import "CAReturnGoodsDetailGoodsView.h"
#import "CAReturnGoodsDetailExpressView.h"
#import "CAExpressListViewController.h"
#import "CASubmitExpressApi.h"

@interface CAReturnGoodsExpressViewController () <CAExpressListViewControllerDelegate>

@property (nonatomic, strong) CAReturnGoodsDetailGoodsView *goodsView;
@property (nonatomic, strong) CAReturnGoodsDetailExpressView *expressView;
@property (nonatomic, strong) UIButton *submitButton;

@end

@implementation CAReturnGoodsExpressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGBCOLOR(241, 241, 241);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.goodsView];
    [self.view addSubview:self.expressView];
    [self.view addSubview:self.submitButton];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"退款详情";
}

- (void)updateViewConstraints {
    [super updateViewConstraints];
    [_goodsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.height.mas_equalTo(120);
    }];
    
    [_expressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.top.equalTo(self.goodsView.mas_bottom).offset(10);
        make.bottom.equalTo(self.submitButton.mas_top);
    }];
    
    [_submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (isIPhoneX) {
            make.bottom.equalTo(self.view).offset(-34);
        } else {
            make.bottom.equalTo(self.view);
        }
        make.height.mas_equalTo(44);
    }];
}

#pragma mark get & set
- (CAReturnGoodsDetailGoodsView *)goodsView {
    if (!_goodsView) {
        _goodsView = [CAReturnGoodsDetailGoodsView new];
        [_goodsView.goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, self.dict[@"goods_img"]]]];
        _goodsView.nameLabel.text = self.dict[@"goods_name"];
        NSArray *attrArray = self.dict[@"attr"];
        __block NSString *attrStr = @"";
        [attrArray enumerateObjectsUsingBlock:^(NSDictionary *attrDict, NSUInteger idx, BOOL * _Nonnull stop) {
            attrStr = [NSString stringWithFormat:@"%@ %@:%@", attrStr, attrDict[@"name"], attrDict[@"attr_value"]];
        }];
        self.goodsView.sizeLabel.text = attrStr;
    }
    return _goodsView;
}

- (CAReturnGoodsDetailExpressView *)expressView {
    if (!_expressView) {
        _expressView = [CAReturnGoodsDetailExpressView new];
        
        NSString *total = [NSString stringWithFormat:@"￥%.2f", [self.dict[@"goods_price"] floatValue] * [self.dict[@"goods_number"] integerValue]];
        _expressView.returnPriceLabel.text = total;
        
        UITapGestureRecognizer *gesture = [UITapGestureRecognizer new];
        [_expressView addGestureRecognizer:gesture];
        [[gesture rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            CAExpressListViewController *expressListVC = [CAExpressListViewController new];
            expressListVC.delegate = self;
            [self.navigationController pushViewController:expressListVC animated:YES];
        }];
    }
    return _expressView;
}

- (UIButton *)submitButton {
    if (!_submitButton) {
        _submitButton = [UIButton new];
        [_submitButton setTitle:@"提交" forState:UIControlStateNormal];
        _submitButton.backgroundColor = RGBCOLOR(233, 85, 19);
        _submitButton.titleLabel.font = [UIFont systemFontOfSize:20];
        
        [[_submitButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self submitAction];
        }];
    }
    return _submitButton;
}

#pragma mark private mehtod
- (void) submitAction {
    NSString *expressName = self.expressView.orderStatusSelectLabel.text;
    if (IsStrEmpty(expressName)) {
        [SVProgressHUD lm_displayErrorWithStatus:@"请选择快递公司"];
        return;
    }
    NSString *expressNo = self.expressView.returnSpeakTextField.text;
    if (IsStrEmpty(expressNo)) {
        [SVProgressHUD lm_displayErrorWithStatus:@"请填写快递单号"];
        return;
    }
    NSString *returnId = [NSString stringWithFormat:@"%ld", [self.dict[@"id"] integerValue]];
    
    CASubmitExpressApi *submitExpressApi = [[CASubmitExpressApi alloc] initWithExpressName:expressName ExpressNo:expressNo ReturnId:returnId];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [submitExpressApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CASubmitExpressApi *api = (CASubmitExpressApi *) request;
        if (api.done) {
            [self.navigationController popViewControllerAnimated:YES];
            [SVProgressHUD lm_displaySuccessWithStatus:@"提交成功，请耐心等待退款"];
        } else  {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

#pragma mark CAExpressListViewControllerDelegate
-(void) selectExpressName:(NSString *) expressName {
    self.expressView.orderStatusSelectLabel.text = expressName;
}

@end
